package com.ruicar.afs.cloud.afscase.creditoption.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.creditoption.condition.CreditOptionCondition;
import com.ruicar.afs.cloud.afscase.creditoption.entity.CreditOption;
import com.ruicar.afs.cloud.afscase.creditoption.entity.CreditOptionInfo;
import com.ruicar.afs.cloud.afscase.creditoption.service.CreditOptionInfoService;
import com.ruicar.afs.cloud.afscase.creditoption.service.CreditOptionService;
import com.ruicar.afs.cloud.afscase.creditoption.vo.OptionInfoVO;
import com.ruicar.afs.cloud.common.core.security.service.AfsUser;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.LabelStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description
 * @Author Peng_Jinbo
 * @Date 2020/06/10 13:09
 */
@RestController
@RequestMapping("/creditOptionInfo")
@Slf4j
@AllArgsConstructor
@Api("信贷选择信息")
public class CreditOptionInfoController {

    private CreditOptionService creditOptionServiceImpl;
    private CreditOptionInfoService creditOptionInfoServiceImpl;

    @GetMapping("/listCreditOptionInfos")
    @ApiOperation("信贷分析->信贷选项获取")
    public IResponse listCreditOptionInfos(@ModelAttribute CreditOptionCondition queryCondition) {
        List<OptionInfoVO> infoVOList = new ArrayList<>();
        // 查询是否存在保存历史
        List<CreditOptionInfo> optionInfos = creditOptionInfoServiceImpl.list(
                Wrappers.<CreditOptionInfo>lambdaQuery().eq(CreditOptionInfo::getApplyNo, queryCondition.getApplyNo())
        );
        // 非审核页面不查配置数据
        if (Objects.nonNull(queryCondition.getCheckPage()) && queryCondition.getCheckPage()) {
            // 不存在保存历史则从信贷选项配置表获取
            if (CollectionUtil.isEmpty(optionInfos)) {
                AfsUser user = SecurityUtils.getUser();
                List<CreditOption> creditOptions = creditOptionServiceImpl.list(
                        Wrappers.<CreditOption>lambdaQuery()
                                .eq(StringUtils.isNotBlank(queryCondition.getShowBusinessType()), CreditOption::getShowBusinessType, queryCondition.getShowBusinessType())
                                .like(CreditOption::getShowPeople, user.getUserRealName())
                                .eq(CreditOption::getStatus, LabelStatusEnum.TAKE_EFFECT.getCode())
                );
                if (CollectionUtil.isNotEmpty(creditOptions)) {
                    for (CreditOption creditOption : creditOptions) {
                        OptionInfoVO optionVO = OptionInfoVO.builder().build();
                        optionVO.setRiskId(Convert.toStr(creditOption.getId()));
                        optionVO.setParameterName(creditOption.getParameterName());
                        optionVO.setDataDicId(creditOption.getDataDicId());
                        optionVO.setControlType(creditOption.getControlType());
                        optionVO.setApplyNo(queryCondition.getApplyNo());
                        infoVOList.add(optionVO);
                    }
                }
            } else {
                for (CreditOptionInfo optionInfo : optionInfos) {
                    OptionInfoVO optionVO = OptionInfoVO.builder().build();
                    BeanUtil.copyProperties(optionInfo, optionVO);
                    infoVOList.add(optionVO);
                }
            }
        }else{
            for (CreditOptionInfo optionInfo : optionInfos) {
                OptionInfoVO optionVO = OptionInfoVO.builder().build();
                BeanUtil.copyProperties(optionInfo, optionVO);
                infoVOList.add(optionVO);
            }
        }
        return IResponse.success(infoVOList);
    }

}
